package com.ysu.zyw.fk.dds;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * DynamicDataSource is a routing data source that plugin all project data source in one,
 * all this is worked by a dynamic data source aspect, when aspect capture an request required
 * one of the data source, then the determineCurrentLookupKey return that data source's name.
 * <p>
 * Created by ZeRur on 2016/1/19.
 *
 * @author yaowu.zhang
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    private static final Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);

    @Override
    protected Object determineCurrentLookupKey() {
        String dataSourceKey = DynamicDataSourceKeyHolder.get();
        logger.debug("Thread '" + Thread.currentThread().getName() + "' use data source '" + dataSourceKey);
        return dataSourceKey;
    }

}
